package cn.hackcloud.netty.basic.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

public class AsyncTimeServerHandler implements Runnable {
	AsynchronousServerSocketChannel serverSocketChannel;

	CountDownLatch countDownLatch;

	public AsyncTimeServerHandler() {
		super();
		try {
			this.serverSocketChannel = AsynchronousServerSocketChannel.open();
			serverSocketChannel = serverSocketChannel.bind(new InetSocketAddress("127.0.0.1", 8888));
			System.out.println("服务启动完成");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		countDownLatch = new CountDownLatch(1);
		doAccept();
		try {
			countDownLatch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void doAccept() {
		serverSocketChannel.accept(this, new AcceptCompletionHandler());

	}

}
